import calendar
import datetime
import random

from flask import Blueprint, current_app
from flask_cors import cross_origin
from flask import request

from app.Services.AdminServices.UserServices.UserServices import UserServices
from app.Services.GoalServices.GoalServices import GoalService
from app.Services.HealthServices.WeightServices.WeightServices import WeightServices
from app.Utils.Decorator.UserIdDecorator import inject_user_id
from app.Utils.Utils import success_response, error_response, return_response_

# 1. 创建蓝图对象
goal_bp = Blueprint(
    name='goal_bp',
    import_name=__name__,
    url_prefix='/goal'
)
goal_services = GoalService()


@goal_bp.route('/get_goals_details', methods=['GET', 'POST'])
@inject_user_id(default_user_id=1)  # 启用装饰器，默认值设为 1
def get_goals_details(user_id):
    """
    获取目标明细。
    """
    filters = {
        'user_id': user_id,
        'start_date_start': request.json.get('start_date', None),
        'start_date_end': request.json.get('end_date', None),
    }
    status, res = goal_services.query_goals(filters)
    return return_response_(status, res)


@goal_bp.route('/add_goal', methods=['POST'])
@inject_user_id(default_user_id=1)  # 启用装饰器，默认值设为 1
def add_goal(user_id):
    """
    新增目标
    """
    user_id = user_id
    goal_data = {'first_cate_id': request.json.get('first_cate_id'),
                 'goal_name': request.json.get('goal_name'),
                 'start_date': request.json.get('start_date'),
                 'end_date': request.json.get('end_date'),
                 'goal_status': 0,
                 'user_id': user_id
                 }
    status, res = goal_services.add_goal(goal_data)
    print(status, res)
    return return_response_(status, res)


# 获取目标一级分类
@goal_bp.route('/get_goal_cate_by_user_id', methods=['GET'])
@inject_user_id(default_user_id=1)  # 启用装饰器，默认值设为 1
def get_goal_cate_by_user_id(user_id):
    """
    根据用户id获取目标一级分类
    """
    user_id = user_id
    status, res = goal_services.get_all_first_cate(user_id)
    return return_response_(status, res)


# 根据目标id修改目标信息
@goal_bp.route('/update_goal', methods=['POST'])
def update_goal():
    """
    根据目标id修改目标信息
    """
    goal_form = request.json
    goal_id = goal_form.get('id')
    status, res = goal_services.update_goal(goal_id, goal_form)
    return return_response_(status, res)


# 删除目标
@goal_bp.route('/delete_goal', methods=['POST'])
def delete_goal():
    """
    根据目标id删除目标信息。
    """
    goal_id = request.json.get('id')
    status, res = goal_services.delete_goal(goal_id)
    return return_response_(status, res)
